package cn.kgc.controller;

import cn.kgc.pojo.DeptInf;
import cn.kgc.pojo.EmployeeInf;

import cn.kgc.pojo.JobInf;
import cn.kgc.service.DeptService;
import cn.kgc.service.EmployeeService;

import cn.kgc.service.JobService;
import cn.kgc.utils.PageInfo;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private JobService jobService;

    @Resource
    private DeptService deptService;


    @RequestMapping("/limit")
    public String limit(@RequestParam(value = "currentPageNo",required = false) Integer currentPageNo,
                        @RequestParam(value = "pageSize",required = false) Integer pageSize,
                        @RequestParam(value = "name",required = false) String name,
                        @RequestParam(value = "cardId",required = false) String cardId,
                        @RequestParam(value = "phone",required = false) String phone,
                        @RequestParam(value = "deptId",required = false) Integer deptId,
                        @RequestParam(value = "jobId",required = false) Integer jobId,
                        @RequestParam(value = "sex",required = false) Integer sex,
                        Model model){

        PageInfo pageInfo = new PageInfo();
        if (currentPageNo !=null){
            pageInfo.setCurrentPageNo(currentPageNo);
        }
        if (pageSize!=null){
            pageInfo.setPageSize(pageSize);
        }
        int totalCount = employeeService.getCount(name,cardId,phone,deptId,jobId,sex);
        pageInfo.setTotalConut(totalCount);
        List<EmployeeInf> list =employeeService.getEmployeeInfByPage(pageInfo.getCurrentPageNo(),pageInfo.getPageSize(),name,cardId,phone,deptId,jobId,sex);
        pageInfo.setData(list);
        List<DeptInf> list1 = deptService.getDeptInfo();
        List<JobInf> list2 = jobService.getJobInfo();
        model.addAttribute("dept12",list1);
        model.addAttribute("job12",list2);
        model.addAttribute("employee",pageInfo);
//        model.addAttribute("name",name);
        model.addAttribute("cardId",cardId);
        model.addAttribute("phone",phone);
        model.addAttribute("deptId",deptId);
        model.addAttribute("jobId",jobId);
        model.addAttribute("sex",sex);

        return "employeeLimit";
    }
    @RequestMapping("/toUpdate")
    public String toUpdate(@RequestParam("id")Integer id,Model model){
        EmployeeInf employeeInf = employeeService.getEmployeeInfById(id);
        List<DeptInf> list1 = deptService.getDeptInfo();
        List<JobInf> list2 = jobService.getJobInfo();
        model.addAttribute("dept12",list1);
        model.addAttribute("job12",list2);
        model.addAttribute("employee",employeeInf);
        return "employeeUpdate";
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(EmployeeInf employeeInf){
        boolean flag = employeeService.updateEmployeeInf(employeeInf);
        if (flag){
            return "redirect:/employee/limit";
        }else {
            return "employeeUpdate";
        }
    }
    @RequestMapping("/toDel")
    public String toDel(@RequestParam("id")Integer id){
        boolean flag = employeeService.delId(id);
        if (flag){
            return "redirect:/employee/limit";
        }else {
            return "employeeLimit";
        }

    }

    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        List<DeptInf> list1 = deptService.getDeptInfo();
        List<JobInf> list2 = jobService.getJobInfo();
        model.addAttribute("dept12",list1);
        model.addAttribute("job12",list2);
        return "employeeAdd";
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(EmployeeInf employeeInf){
        boolean flag = employeeService.addEmployeeInf(employeeInf);
        if (flag){
            return "redirect:/employee/limit";
        }else {
            return "index";
        }
    }

    @RequestMapping("/emoloyeeIsExist/{name}")
    @ResponseBody
    public JSONObject emoloyeeIsExist(@PathVariable String name) {

        EmployeeInf employeeInf = employeeService.getEmployeeInfName(name);
        JSONObject jsonObject = new JSONObject();
        if (employeeInf == null) {
            jsonObject.put("code", "10000");
            jsonObject.put("msg", "可以使用");
        } else {
            jsonObject.put("code", "10001");
            jsonObject.put("msg", "不可以使用");
        }
        return jsonObject;
    }
}
